//
// Parse.cpp for  in /home/goldbe_y//cpp/Maths/206observ
//
// Made by yoni goldberg
// Login   <goldbe_y@epitech.net>
//
// Started on  Mon May  2 15:13:56 2011 yoni goldberg
// Last update Mon Oct 22 17:18:06 2012 yoni goldberg
//

#include <cstdlib>

#include "Parse.hpp"
#include "convert.hpp"

std::string	Parse::epur(std::string tmp)
{
  int	i = 0;

  while (tmp[i])
    {
      if (tmp[i] == '\t')
	tmp[i] = ' ';
      i++;
    }
  while (tmp[i] == ' ')
    tmp.erase(i, 1);
  return (tmp);
}

Parse::Parse(const std::string &str)
{
  std::string			temp(" ");
  std::ifstream			file(str.c_str());

  if (file.is_open())
    {
      getline(file, temp);
      getline(file, temp);
      _dim = string_to_var<int>(temp);
      _vecteur = new float[_dim];
      _matrix = new float*[_dim];
      getline(file, temp);
      for (int i = 0; i < _dim; i++)
	{
	  _matrix[i] = new float[_dim];
	  getline(file,temp);
	  _tab.push_back(epur(temp));
	}
      getline(file, temp);
      for (int i = 0; i < _dim; i++)
	{
	  getline(file, temp);
	  _vecteur[i] = string_to_var<float>(temp);
	}
    }
  fill_matrix();
}

Parse::~Parse()
{
}

int		Parse::get_dim() const
{
  return _dim;
}

float		**Parse::get_mat() const
{
  return _matrix;
}

float		*Parse::get_vecteur() const
{
  return _vecteur;
}

void		Parse::fill_matrix()
{
  for (int i = 0; i < _dim; i++)
    {
      int	count = 0;
      for (int j = 0; j < _tab[i].size(); j++)
	{
	  std::string temp("");
	  while (_tab[i][j] == ' ') j++;
	  while (_tab[i][j] >= '0' && _tab[i][j] <= '9') temp += _tab[i][j++];
	  _matrix[i][count] = string_to_var<float>(temp);
	  count++;
	}
    }
}
